<template>
  <el-card class="box-card">
    <el-button
      type="primary"
      :disabled="!category3Id"
      icon="el-icon-plus"
      @click="addAttr"
    >
      添加属性
    </el-button>
    <el-table v-loading="loading" :data="attrList" border style="width: 100%">
      <el-table-column type="index" align="center" label="序号" width="50" />
      <el-table-column prop="attrName" label="属性名" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            v-for="valueList in row.attrValueList"
            :key="valueList.id"
            class="attr-tag"
          >
            {{ valueList.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-tooltip effect="dark" content="修改属性值" placement="top-start">
            <el-button
              type="warning"
              icon="el-icon-edit"
              size="mini"
              @click="editAttr(row)"
              class="attrlist-button"
            />
          </el-tooltip>

          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="deleteAttr(row)"
          >
            <el-tooltip
              slot="reference"
              effect="dark"
              content="删除属性值"
              placement="top-start"
            >
              <el-button type="danger" icon="el-icon-delete" size="mini" />
            </el-tooltip>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { reqGetAttrList, reqDeleteAttr } from '@/api/product/attr'
export default {
  data() {
    return {
      loading: false,
      attrList: []
    }
  },
  computed: {
    ...mapState('category', ['category1Id', 'category2Id', 'category3Id'])
  },
  watch: {
    category3Id: {
      async handler(category3Id) {
        // 判断3id是是否有值,从无变有发请求,从有变无不发
        if (!category3Id) {
          this.attrList = []
          return
        }
        this.getAttrList()
      },
      immediate: true
    }
  },
  methods: {
    ...mapMutations('category', ['CLEAR_ALL_ATTR']),
    // 修改属性值
    editAttr(row) {
      this.$emit('setAttrList', row)
      // // 切换到添加属性的页面
      this.$emit('setIsShowAttrList', false)
    },
    // 添加属性时重置一下attr
    addAttr() {
      // 重置添加属性值模块的值
      this.$emit('setAttrList', { attrName: '', attrValueList: [] })
      // 跳转到添加属性值
      this.$emit('setIsShowAttrList', false)
    },
    // 删除属性值
    async deleteAttr(row) {
      // 删除属性
      await reqDeleteAttr(row.id)
      this.$message({
        type: 'success',
        message: '删除属性成功'
      })
      // 删除后重新发送请求获取数据
      this.getAttrList()
    },
    // 发送请求获取数据列表
    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this
      this.loading = true
      const { data } = await reqGetAttrList({
        category1Id,
        category2Id,
        category3Id
      })
      this.loading = false
      this.attrList = data
    }
  }
}
</script>

<style lang="scss" scoped>
.attr-tag {
  margin-right: 10px;
  margin-bottom: 5px;
}
.attrlist-button {
  margin-right: 10px;
}
</style>
